#ifndef __ILIB_CORE_RING_QUEUE_H__
#define __ILIB_CORE_RING_QUEUE_H__

#include <list>

/**	@brief one way pipe for high performance communication between threads
 *	
 *	@Detail
 *		if write cursor == read cursor means the queue is empty
 *		if write cursor == read curosr-1 means the queue is full
 */

template<class T>
class iRingQueue
{
	typedef T* Traint;
public:
	iRingQueue(int length=1024)
	{
		m_buffer_start = m_read_cursor = m_write_cursor = new Traint[length];
		m_buffer_end = m_buffer_start+length;
	}
	T* pop()
	{
		Traint* curW = m_write_cursor;
		Traint* curR = m_read_cursor;
		if(curW == curR) return NULL;
		T* v = *curR++;
		if(curR == m_buffer_end)
			curR = m_buffer_start;
		m_read_cursor = curR;
		return v;

	}
	void push(T* data)
	{
		Traint* curW = m_write_cursor;
		Traint* curR = m_read_cursor;
		if(curW+1 == curR) return;
		if(curW==m_buffer_end && curR==m_buffer_start) return;
		*curW++ = data;
		if(curW == m_buffer_end) curW = m_buffer_start;
		m_write_cursor = curW;
	}

protected:
	Traint* m_buffer_start;
	Traint* m_buffer_end;
	Traint* m_read_cursor;
	Traint* m_write_cursor;

	std::list<Traint> m_wait_flush;
};

#endif  //__ILIB_CORE_RING_QUEUE_H__
